package com.android.kotlincoroutinemvvmdemo.view

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.AnticipateInterpolator
import android.view.animation.LinearInterpolator
import com.android.kotlincoroutinemvvmdemo.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope

class SevenCircleProgressView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : View(context, attributeSet), CoroutineScope by MainScope() {

    private var mWidth = 0
    private var mHeight = 0

    private var mStartAngle = -90F

    private var startAngle: Float = mStartAngle
        set(value) {
            field = value
            invalidate()
        }

    private val angleArray = floatArrayOf(
        0F,
        20F,
        40F,
        60F,
        80F,
        100F,
//        120F,
//        140F,
//        160F,
//        180F,
//        200F,
//        220F,
//        240F,
//        260F,
//        280F,
//        300F,
//        320F,
//        340F
    )

    private val angleArray0 = floatArrayOf(
//        340F,
//        320F,
//        300F,
//        280F,
//        260F,
//        240F,
//        220F,
//        200F,
//        180F,
//        160F,
//        140F,
//        120F,
        100F,
        80F,
        60F,
        40F,
        20F,
        0F
    )

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = context.resources.getColor(R.color.color_ffeb3b)
            style = Paint.Style.FILL
        }
    }

    private val radius = context.resources.getDimension(R.dimen.dp_3)

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        mWidth = when (MeasureSpec.getMode(widthMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(widthMeasureSpec)
            }

            else -> {
                context.resources.getDimension(R.dimen.dp_100).toInt()
            }
        } - paddingStart - paddingEnd

        mHeight = when (MeasureSpec.getMode(heightMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(heightMeasureSpec)
            }

            else -> {
                context.resources.getDimension(R.dimen.dp_100).toInt()
            }
        } - paddingTop - paddingBottom

        setMeasuredDimension(mWidth, mHeight)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.apply {
            translate(mWidth / 2.0F, mHeight / 2.0F)
            for (i in angleArray.indices) {
                canvas.save()
                //角度值为正，表示顺时针旋转
                rotate(angleArray[i] + startAngle)
                //角度值为负，表示逆时针旋转
                //rotate(-angleArray[i] + startAngle)
                canvas.drawCircle(mWidth / 3.0F, 0F, radius, mPaint0)
                canvas.restore()
            }
        }
    }

    fun startAnimation() {
        //顺时针旋转
        val objectAnimator =
            ObjectAnimator.ofFloat(this, "startAngle", 0F, 360F)
        //逆时针旋转
        //val objectAnimator =
        //    ObjectAnimator.ofFloat(this, "startAngle", -0F, -360F)
        objectAnimator.addUpdateListener { animation -> {}}
        objectAnimator.duration = 2000
        //使用差值器避免中间会出现的停顿
        objectAnimator.interpolator = LinearInterpolator()
        //objectAnimator.interpolator = AccelerateDecelerateInterpolator()
        objectAnimator.repeatMode = ValueAnimator.RESTART
        objectAnimator.repeatCount = ValueAnimator.INFINITE
        objectAnimator.start()
    }

    companion object {
        const val TAG = "AnimationView"
    }
}